(ns cljstalk2011.sillybench
   (:require [goog.dom :as dom]) ; require a JS-source library
   ; this seems to be how you get macros...
   (:require-macros [cljstalk2011.util :as util]))

; dom-appending prn-alike
(defn pn [text]
  (let [output (dom/getElement "output")]
  (dom/append output (dom/createDom "div" (.strobj {"class" "print"}) (str text)))))

(defn std-abw [xs]
  (let [n     (count xs)
        mw    (/ (reduce + xs) n)
        quads (map #(Math/pow (- % mw) 2) xs)]
    (Math/sqrt (/ (reduce + quads) (dec n)))))

(defn do-benchmark [print-fn]
   (dotimes [_ 100]
   (print-fn (util/js-time (std-abw (vec (take 10000
                         (repeatedly
                          #(rand-int 100)))))))))

(defn ^{:export htmlbench} html-bench []
   (do-benchmark pn))

(defn -main [& args]
   (do-benchmark prn))

(set! *main-cli-fn* -main)
